<?php
	if (isset($_POST['query']) && isset($_POST['tipo'])){
		if ($_POST['tipo'] == "buscarLibros"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$sql = $_POST['query'];
			$result = mysql_query($sql) or die("Error en consulta ".mysql_error());
			$json = array();
			if(mysql_num_rows($result)){
				while($row=mysql_fetch_assoc($result)){
					$json['result'][]=$row;
				}
			}
			mysql_close($db);
			echo json_encode($json); 
		}
		if ($_POST['tipo'] == "listaCompra"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$sql = $_POST['query'];
			$result = mysql_query($sql) or die("Error en consulta ".mysql_error());
			$json = array();
			if(mysql_num_rows($result)){
				while($row=mysql_fetch_assoc($result)){
					$json['result'][]=$row;
				}
			}
			mysql_close($db);
			echo json_encode($json); 
		}
		else if ($_POST['tipo'] == "registro"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$sql = $_POST['query'];
			$result = mysql_query($sql);
			/*
			$json = array();
			if(mysql_num_rows($result)){
				while($row=mysql_fetch_assoc($result)){
					$json['result'][]=$row;
				}
			}
			*/
			mysql_close($db);
			if ($result){
				echo "registro_correcto";
			}
			else{
				echo "error";
			}
		}
		else if ($_POST['tipo'] == "login"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$sql = $_POST['query'];
			$result = mysql_query($sql) or die("Error en login ".mysql_error());
			$num_r = mysql_num_rows($result);
			$json = array();
			if(mysql_num_rows($result)){
				while($row=mysql_fetch_assoc($result)){
					$json['result'][]=$row;
				}
			}
			mysql_close($db);
			if ($num_r == 1){
				echo json_encode($json);
			}
			else{
				echo "error";
			}
		}
		
		else if ($_POST['tipo'] == "agregar_pedido"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$vec = explode(';', $_POST['query']);
			$email = $vec[0];
			$idlibro = $vec[1];
			$contiene = (int)$vec[2];
			
			$sql = "INSERT INTO pedido (idLibro, email, contiene, estado, fecha)
						VALUES ($idlibro,'$email', $contiene, DEFAULT, 'fecha')
						ON DUPLICATE KEY UPDATE contiene = $contiene";
			$result = mysql_query($sql) or die("Error en agregar_pedido ".mysql_error());
			
			mysql_close($db);
			if ($result){
				echo "pedido_agregado_exito";
				//echo "$contiene";
			}
			else{
				echo "error";
			}
		}
		
		else if ($_POST['tipo'] == "realizar_pedido"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$email = $_POST['query'];
			
			
			$sql = "UPDATE pedido
					SET estado='aceptado'
					WHERE email LIKE '$email'";
			$result = mysql_query($sql) or die("Error en agregar_pedido ".mysql_error());
			
			mysql_close($db);
			if ($result){
				echo "pedido_realizado_exito";
				//echo "$contiene";
			}
			else{
				echo "error";
			}
		}
		
		else if ($_POST['tipo'] == "borrar_pedido"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$vec = explode(';', $_POST['query']);
			$email = $vec[0];
			$idlibro = $vec[1];
			
			$sql = "DELETE FROM pedido
					WHERE email LIKE '$email' AND idLibro LIKE $idlibro";
			$result = mysql_query($sql) or die("Error en borrar_pedido".mysql_error());
			
			mysql_close($db);
			if ($result){
				echo "pedido_borrado_exito";
				//echo "$contiene";
			}
			else{
				echo "error";
			}
		}
		
		else if ($_POST['tipo'] == "actualizar"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$sql = $_POST['query'];
			$result = mysql_query($sql) or die("Error en consulta ".mysql_error());
			
			mysql_close($db);
			if($result){
				echo "actualizar_exito";
			}
			else{
				echo "error";
			}
			
		}
		
		else if ($_POST['tipo'] == "borrar_lista_compra"){
			$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
			$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
			$email = $_POST['query'];
			$sql = "DELETE FROM pedido
					WHERE email LIKE '$email'";
			$result = mysql_query($sql) or die("Error en consulta ".mysql_error());
			
			mysql_close($db);
			if($result){
				echo "borrar_lista_exito";
			}
			else{
				echo "error";
			}
			
		}
		
	}
	else{ // Funciona con el navegador
		echo "Buenos dias. Soy el server PHP.\n"; // *** DEBUG****
		$db=mysql_connect("sighton.es.mysql", "sighton_es", "eltitojorge") or die("cannot connect"); // *** DEBUG****
		$sel = mysql_select_db("sighton_es", $db) or die ("cannot select DB");
		$sql = $_POST['query'];
		echo "DB = $db\n"; // *** DEBUG****
		echo "SEL = $sel\n"; // *** DEBUG****
		$sql = "select * from libro"; // *** DEBUG****
		$result = mysql_query($sql);
		echo "RESULT = $result\n"; // *** DEBUG****
		$json = array();
		 
		if(mysql_num_rows($result)){
			while($row=mysql_fetch_assoc($result)){
				$json['result'][]=$row;
			}
		}
		mysql_close($db);
		echo json_encode($json); 
	}
	
?>